home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / console / svgatext.3 / svgatext / SVGATextMode-1.3 / XFREE / common_hw / Ch8391clk.c next >
Encoding:
C/C++ Source or Header  |  1995-10-29  |  9.8 KB  |  321 lines

  1. /* $XConsortium: Ch8391clk.c,v 1.1 95/01/23 15:03:54 kaleb Exp $ */
  2. /* $XFree86: xc/programs/Xserver/hw/xfree86/common_hw/Ch8391clk.c,v 3.3 1995/07/01 10:48:55 dawes Exp $ */
  3. /*
  4.  * Copyright 1995 The XFree86 Project, Inc
  5.  *
  6.  * programming the clock chip Chrontel 8391, derived from the
  7.  * chrontel8391 program from Richard Burdick <burdir@fionn.cuug.ab.ca>
  8.  * Harald Koenig  <koenig@tat.physik.uni-tuebingen.de>
  9.  */
  10.  
  11. /*
  12.  *
  13.  * This is a clock setting program for the chrontel 8391 RAMDAC with
  14.  * programmable clock.  It may work with the chrontel 8398 RAMDAC
  15.  * with programmable clock.  (I don't have one to test, but if you want
  16.  * you can try it)
  17.  *
  18.  * Copyright (C) 1994 Richard Burdick
  19.  *
  20.  * Richard Burdick <burdir@fionn.cuug.ab.ca>
  21.  * 16 December 1994
  22.  *
  23.  * modified to fit into XFree86 source tree by
  24.  * Harald Koenig  <koenig@tat.physik.uni-tuebingen.de>
  25.  * 14 January 1995
  26.  *
  27.  */
  28.  
  29.  
  30.  
  31. /* current status:
  32.  
  33. version 0.1
  34.  
  35.    This is basically very experimental.  Currently I am using it and have had
  36.    no problems.  I was inspired to write this because I got tired of looking
  37.    at XFree86 at 640x480.  Now I am looking at it at 1024x768.  If you want to
  38.    try it, go ahead.  As expressed above, I make no claims of warranty,
  39.    so if your machine blows up, you've been warned.
  40.  
  41.    I am releasing this right now because I was so damn tired of 640x480
  42.    and wanted higher resolutions as soon as possible and figured others
  43.    would as well
  44.  
  45.    I am still making modifications so I ask that if you use this,
  46.    drop me a line so I can keep people up to date.
  47.  
  48.    If you modify this please inform me (especially if you had to modify it to
  49.    get it to compile on your system) so I can put changes back into my version.
  50.  
  51.    If you know anything about programming the S3 please feel free to
  52.    contact me if you have any suggestions about how I could do this better
  53.  
  54.    It works under Linux, and probably anything else XFree86 works on (I hope!)
  55.  
  56.    it has to be made suid root, as it has to call ioperm to use the video
  57.    ports
  58. */
  59.  
  60. #include "compiler.h"
  61. #define NO_OSLIB_PROTOTYPES
  62. #include "xf86_OSlib.h"
  63.  
  64. extern int vgaIOBase;
  65.  
  66.  
  67. /* when RS2 = 0 */
  68.  
  69. #define PALETTE_RAM_WRITE_ADDRESS            0x03C8
  70. #define PALETTE_RAM_DATA_REGISTER            0x03C9
  71. #define PIXEL_READ_MASK_REGISTER             0x03C6
  72. #define PALETTE_RAM_READ_ADDRESS             0x03C7
  73.  
  74. /* when RS2 = 1 */
  75. #define CLOCK_RAM_WRITE_ADDRESS              0x03C8
  76. #define CLOCK_RAM_DATA_REGISTER              0x03C9
  77. #define CONTROL_REGISTER                     0x03C6
  78. #define CLOCK_RAM_READ_ADDRESS               0x03C7
  79.  
  80. #define FREQ_MIN   8500
  81. #define FREQ_MAX 135000
  82. #define CHRONTEL_REF_FREQ 14.31818
  83.  
  84.  
  85. /* 
  86.    the chrontel8391 and 8398 have 16 video clocks and 8 memory clocks.
  87.    we should use clock 2 because on 8398 clock 0 is hardwired to 25.175 and
  88.    clock 1 is hardwired to 28.322 (on 8391 nothing is hardwired).
  89.    */
  90.  
  91. #if NeedFunctionPrototypes
  92. static void 
  93. s3ProgramChrontel8391Clock(unsigned char m, unsigned char n, unsigned char k, unsigned int clk)
  94. #else
  95. static void s3ProgramChrontel8391Clock(m, n, k, clk)
  96. unsigned char m;
  97. unsigned char n;
  98. unsigned char k;
  99. unsigned int clk;
  100. #endif
  101. {
  102.    unsigned char tmp, oldCR55, oldPLLwrite, oldCSR, CSR = 0x00;
  103.    int vgaCRAddr, vgaCRData;
  104.  
  105.    vgaCRAddr = vgaIOBase + 4;
  106.    vgaCRData = vgaIOBase + 5;
  107.  
  108.    /* RS2 controlled in CR55 bit 0 */
  109.    outb(vgaCRAddr, 0x55);
  110.    oldCR55 = inb(vgaCRData);   /* save value in register 55 */
  111.    /* set bit 0 of CR55 to 1 to set RS2 to 1 to make sure we are
  112.       talking to correct registers */
  113.    outb(vgaCRData, (oldCR55 & 0xFC) | 0x01);
  114.  
  115.    /* save PLL write address, then load it to write to clock "clk" */
  116.    oldPLLwrite = inb(CLOCK_RAM_WRITE_ADDRESS);
  117.  
  118.    outb(CLOCK_RAM_WRITE_ADDRESS, clk);
  119.  
  120.    /* to write the PLL ram, we load the address into the write address
  121.       register, then load the lsb into the pll ram data register, then
  122.       load the msb into the pll ram data register.  the chip automatically
  123.       makes the first write to the address the lsb, and the second the
  124.       write to the msb.
  125.  
  126.       the pll ram is set up like this
  127.  
  128.       Address       LSB      MSB
  129.       00H           VPLL    VPLL
  130.       01H           VPLL    VPLL
  131.       ...
  132.       0FH           VPLL    VPLL
  133.       10H           MPLL    MPLL
  134.       11H           MPLL    MPLL
  135.       ...
  136.       17H           MPLL    MPLL
  137.  
  138.       VPLL refers to the fact that these values are video pll (phase locked
  139.       loop) entries (commonly referred to as dot-clocks)
  140.  
  141.       MPLL refers to the fact that these values are memory pll entries
  142.       (memory clocks).  I don't touch these yet, as I have no idea
  143.       what values they should have, or even what they are for
  144.       (I assume DRAM refresh speed, but I'm not sure)
  145.  
  146.    */
  147.  
  148.    outb(CLOCK_RAM_DATA_REGISTER, n);
  149.    outb(CLOCK_RAM_DATA_REGISTER, (k << 6) | m );
  150.  
  151.    /* there, we've just changed clock "clk", now we have to select it */
  152.  
  153.    /* first, read the clock select on the chip */
  154.    /* I'll use the alternate access method of accessing the chips clock
  155.       select register, which is to read the clock write address register
  156.       four times in a row without reading any other clock register.
  157.       the fifth access (read or write) is the clock select register
  158.       (Start by reading some other register to reset sequence)
  159.    */
  160.  
  161.  
  162.    /* first read the clock select */
  163.    (void)inb(CONTROL_REGISTER);   /* reset sequence just to make sure */
  164.    (void)inb(CLOCK_RAM_WRITE_ADDRESS);
  165.    (void)inb(CLOCK_RAM_WRITE_ADDRESS);
  166.    (void)inb(CLOCK_RAM_WRITE_ADDRESS);
  167.    (void)inb(CLOCK_RAM_WRITE_ADDRESS);
  168.    oldCSR = inb(CLOCK_RAM_WRITE_ADDRESS);
  169.  
  170.    /* the format of the clock select is: bit 7 is frequency hold
  171.       if this is 0, the rest of the bits are ignored
  172.       if this is 1, then:
  173.          bits 6-4 select the Memory clock, (MPLL)
  174.      bits 3-0 select the video clock (VPLL) (dot-clock)
  175.      the VPLL is logically 'or'ed with the external FS[3:0] bits
  176.      (I assume these are controlled through Register 0x42)
  177.      so the strategy is:
  178.         get the clock select register, do not touch the memory clock
  179.         select bits as we don't know how to use the MPLL, but
  180.         make sure the VPLL select bits are all 0, so the clock select
  181.         is entirely based on the S3's register 0x42 (we select the clock
  182.         through 0x42 later)
  183. */
  184.  
  185.    if ((oldCSR & 0x0F) != 0x00)/* if bits 3-0 already 1, don't bother modifying */
  186.      {
  187.        CSR = oldCSR | 0x80;
  188.        (void)inb(CONTROL_REGISTER);   /* reset sequence just to make sure */
  189.        (void)inb(CLOCK_RAM_WRITE_ADDRESS);  /* do 4 useless reads */
  190.        (void)inb(CLOCK_RAM_WRITE_ADDRESS);
  191.        (void)inb(CLOCK_RAM_WRITE_ADDRESS);
  192.        (void)inb(CLOCK_RAM_WRITE_ADDRESS);
  193.        outb(CLOCK_RAM_WRITE_ADDRESS, CSR);/* fifth access is clock select */
  194.      }
  195.  
  196.    /* that's done, now select clock through register 0x42 on S3 */
  197.  
  198.    tmp = inb(0x3CC);
  199.    outb(0x3C2, tmp | 0x0C);
  200.    outb(vgaCRAddr, 0x042);
  201.    outb(vgaCRData, clk);
  202.  
  203.  
  204.    /* we're all done (I think), put things back the way they were */
  205.  
  206.    /* write the clock ram write address back */
  207.    outb(CLOCK_RAM_WRITE_ADDRESS, oldPLLwrite);
  208.  
  209.    /* put the value of 0x55 back */
  210.    /* if we don't do this, and the server doesn't set CR55 and assumes it
  211.       is okay, when it tries to write to the DAC it will the clock registers
  212.    */
  213.  
  214.    outb(vgaCRAddr, 0x55);
  215.    outb(vgaCRData, oldCR55);
  216. }
  217.  
  218.  
  219. /*
  220.    the formula for the clock is
  221.  
  222.    freq desired  =  reference_freq * n  /  m * k
  223.  
  224.    reference_freq comes from the oscillator on the card (assume 14.31818 Mhz)
  225.    n = N + 8
  226.    m = M + 2
  227.    k = 2^K
  228.  
  229.    we use N, M, K to program the chip
  230.  
  231.    the chrontel literature says: some values of N are restricted, including:
  232.    0-7, 10-15, 19-23, 28-31, 37-39, 46-47, 55.  Why this is, I don't know.
  233.    the 'including' remark makes me think there might be others, but they
  234.    don't say which ones.  Notice that the ranges keep getting smaller
  235.    ie: 0-7, 8 values are restricted.  10-15, 6 values are restricted.
  236.    19-23, 5 values ; 28-31, 4 values; 37-39, 3 values; 46-47, 2; 55, 1
  237.    if I have interpreted this pattern correctly, there aren't any other
  238.    values, but then I could be wrong
  239.  
  240.    the chrontel literature also says use of M values of 10 or less for
  241.    best circuit performance
  242.  
  243. */
  244.  
  245.  
  246.  
  247. #if NeedFunctionPrototypes
  248. void Chrontel8391SetClock(long freq, int clk)
  249. #else
  250. void
  251. Chrontel8391SetClock(freq, clk)
  252. long freq;
  253. int clk;
  254. #endif
  255. {
  256.    double ffreq;
  257.    int n, nmin, nmax, k, m, m0;
  258.    int best_n, best_m;
  259.    double  diff, mindiff;
  260.  
  261.    if (freq < FREQ_MIN)
  262.       ffreq = FREQ_MIN / 1000.0;
  263.    else if (freq > FREQ_MAX)
  264.       ffreq = FREQ_MAX / 1000.0;
  265.    else
  266.       ffreq = freq / 1000.0;
  267.  
  268.    /* work out suitable timings */
  269.  
  270.    /* pick the right p value */
  271.  
  272.    for(k=0; k<4 && ffreq <= 67.5; k++)
  273.       ffreq *= 2;
  274.  
  275.    /* now 67.5 <= ffreq <= 135.0 */
  276.  
  277.    ffreq /= CHRONTEL_REF_FREQ;
  278.  
  279.    /* now 4.7142 <= ffreq <= 9.4285 */
  280.    /* the remaining formula is  ffreq = (n+8) / (m+2) */
  281.  
  282.    nmin = (int)(ffreq * (1+2)) -8 -1;
  283.    if (nmin<8) nmin = 8;  /* because (n <= 7) isn't allowed */
  284.    nmax = (int)(ffreq * (32+2)) -8 +1;
  285.    if (nmax > 255) nmax = 255;
  286.    
  287.    mindiff = ffreq;
  288.    for (n = nmin; n <= nmax; n++) {
  289.       if ( /* (n <= 7) || */
  290.       (n >= 10 && n <= 15) ||
  291.       (n >= 19 && n <= 23) || (n >= 28 && n <= 31) ||
  292.       (n >= 37 && n <= 39) || (n == 46) || (n == 47) ||
  293.       (n == 51))
  294.      continue;    /* the above numbers are not allowed, skip */
  295.       m0 = (int)((n+8) / ffreq) - 2;
  296.       for (m=m0-1; m<=m0+1; m++) {
  297.      if (m<1 || m>31) continue;
  298.     
  299.      diff = (n+8.0) / (m+2) - ffreq;
  300.      if (diff<0)
  301.         diff = -diff;
  302.     
  303.      if (diff < mindiff) {
  304.         mindiff = diff;
  305.         best_n = n;
  306.         best_m = m;
  307.      }
  308.       }
  309.    }
  310. #ifdef DEBUG
  311.    diff = freq/1000.0 - (CHRONTEL_REF_FREQ * (best_n+8.0) / ((best_m + 2) * (1 << k)));
  312.    if (diff<0) diff = -diff;
  313.    ErrorF("clk %d, setting to %12f, m %3d, n %3d, k %d, err %12f\n", clk,
  314.       CHRONTEL_REF_FREQ * (best_n+8.0) / ((best_m + 2) * (1 << k)),
  315.       best_m, best_n, k, diff);
  316. #endif
  317.  
  318.    s3ProgramChrontel8391Clock(best_m, best_n, k, clk);
  319.    return;
  320. }
  321.